package com.example.madman.yuejian.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.madman.yuejian.Adapter.ShowPicAdapter;
import com.example.madman.yuejian.Bean.ShowpicBean;
import com.example.madman.yuejian.Listener.RetrofitListener;
import com.example.madman.yuejian.Model.ShowpicModel;
import com.example.madman.yuejian.R;

import java.util.ArrayList;
import java.util.List;


public class ShowPicFragment extends Fragment {
    private RecyclerView recyclerView;
    private ShowPicAdapter showPicAdapter;
    private List<ShowpicBean> list;
    private RecyclerView.LayoutManager layoutManager;
    private Context context;
    private RetrofitListener<List<ShowpicBean>> listener;
    private ShowpicModel model;

    public ShowPicFragment() {
    }


    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item_showpiclist, container, false);
        context=getActivity();
        recyclerView=(RecyclerView)view.findViewById(R.id.list_showpic);
        list=new ArrayList<ShowpicBean>();
        initdata();
        init();
        connecthttp();
        return view;
    }

    private void initdata(){
        listener = new RetrofitListener<List<ShowpicBean>>() {
            @Override
            public void onSuccess(List<ShowpicBean> showpicBeen) {
                list = showpicBeen;
                showPicAdapter.setList(list);
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
            }
        };
    }
    private void init(){
        layoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        showPicAdapter = new ShowPicAdapter(context);
        showPicAdapter.setList(list);
        recyclerView.setAdapter(showPicAdapter);
    }
    private void connecthttp(){
        model=new ShowpicModel("gson");
        model.getShowpicList(listener);
    }



}
